package servlet;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import bean.UserBean;
import dao.AnimalDao;

/**
 * Servlet implementation class AddAnimalServlet
 */
@WebServlet("/add")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10,      // 10MB
maxRequestSize=1024*1024*50)   // 50MB
public class AddAnimalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// 储存图片的地址
	private static final String FILE_PATH = "C:/Users/14344/eclipse-workspace/cats_dogs/WebContent/files";
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddAnimalServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		UserBean now_user = (UserBean) request.getSession().getAttribute("now_user");

		// 处理表单中的文件
		String filename = "";
		for (Part part : request.getParts()) {
			String contentDisp = part.getHeader("content-disposition");
			String[] items = contentDisp.split(";");
			for (String s : items) {
				if (s.trim().startsWith("filename")) {
					System.out.println(s.substring(s.indexOf("=") + 2, s.length() - 1));
					
//					filename = now_user.getUser_phone() + "_" + s.substring(s.indexOf("=") + 2, s.length() - 1);
					// 临时使用，避免每次都要登录
					filename =s.substring(s.indexOf("=") + 2, s.length() - 1);
					// 保存到服务器
					part.write(FILE_PATH + File.separator +filename);
				}
			}
		}
		
		String a_title = request.getParameter("a_title");
		String a_type = request.getParameter("a_type");
		String a_sex = request.getParameter("a_sex");
		String a_age = request.getParameter("a_age_year")+"-"+ request.getParameter("a_age_month");
		String a_location = request.getParameter("a_location_pro") +"-"+request.getParameter("a_location_city")+"-"+ request.getParameter("a_location_area");
		String a_detail_local = request.getParameter("a_detail_local");
		String a_contact = request.getParameter("a_contact");
		String a_phone = request.getParameter("a_phone");
		String a_desc = request.getParameter("a_desc");
		String a_pic = filename;
		String a_status = "0";
		// 作者
//		String a_auth = now_user.getUser_phone();
		// 临时使用，避免每次都要登录
		String a_auth = "11111111";
		
		
		AnimalDao.add(a_title, a_type, a_sex, a_age, a_location, a_detail_local, a_contact, a_phone, a_desc, a_pic, a_auth, a_status);
		
		request.setAttribute("msg", "登记成功！");
		request.getRequestDispatcher("/add.jsp;").forward(request, response);	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
